'''

Case Type： 数据库系统
Case Name： 进程僵死再恢复
Create At:  2020/06/23
@zou_jialiang0505328126
Descption:  1.执行TPCC事务2.在事务执行过程中暂停主进程3.3.	恢复主进程

history：
created： 2020/06/23  created by @wan005
'''
import os
import unittest
from yat.test import Node
import time
import _thread
from yat.test import macro

import sys

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.ComThread import *
from testcase.utils.Constant import *


class HungDbPid(unittest.TestCase):
    sh = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case116.py start------------')
        self.rootNode = Node()
        self.dbUserNode = Node(node='dbuser')
        self.PID_FILE_NAME = os.path.join(macro.DB_INSTANCE_PATH, macro.PID_FILE_NAME)
        self.TPCC_PATH = macro.TPCC_PATH
        self.com = Common()
        self.Constant = Constant()

    def test_Hung_db_pid(self):
        tpcc_thread = ComThread(self.com.startTPCC, args=(self.rootNode, self.TPCC_PATH,))
        tpcc_thread.start()
        time.sleep(5)

        # STOP opengauss PID
        self.log.info("STOP opengauss PID")
        # get_pid_cmd = 'lsof -i:{port} | grep gaussdb'.replace('{port}', self.rootNode.db_port)
        get_pid_cmd = 'lsof -i:{port}'.replace('{port}', self.rootNode.db_port)
        pid_msg = self.rootNode.sh(get_pid_cmd).result()
        self.log.info(pid_msg)
        pid = self.com.parsePidList(pid_msg)
        self.rootNode.sh('kill -STOP ' + pid[0])

        time.sleep(50)

        # CONNECT opengauss PID
        self.log.info("reconnect opengauss PID")
        self.rootNode.sh('kill -CONT ' + pid[0])

        tpcc_thread.join(timeout=int(macro.TPCC_RUN_MAX_TIME) * 2)

        tpcc_result = tpcc_thread.get_result()
        self.log.info(tpcc_result)
        self.assertIn(self.Constant.TPCC_SUCCESS_MSG, tpcc_result)

    def tearDown(self):
        self.log.info('this is tearDown')
